package com.smart.home.mapper;

import com.smart.home.annotation.autoFill;
import com.smart.home.entity.HomeMembers;
import com.smart.home.enumeration.OperationType;
import com.smart.home.vo.HomeMembersVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface HomeMemberMapper {


    /**
     * 添加
     * @param homeMembers
     */
    @autoFill(value = OperationType.INSERT)
    void CreateHomeMember(HomeMembers homeMembers);

    /**
     * 修改
     * @param homeMembers
     */
    @autoFill(value = OperationType.INSERT)
    void JoinHomeMember(HomeMembers homeMembers);

    /**
     * 根据家庭id和目标用户id返回相关权限
     * @param homeId
     * @param TargetUserId
     * @return
     */
    @Select("select role from smarthome.home_members where home_id = #{homeId} and user_id = #{TargetUserId}")
    String GetRoleByHomeIdAndUserId(Long homeId,Long TargetUserId);

    /**
     * 修改角色
     * @param homeId
     * @param targetUserId
     * @param role
     */
    @Update("update smarthome.home_members set role = #{role} where home_id = #{homeId} and user_id =#{targetUserId}")
    void GrantUser(Long homeId, Long targetUserId, String role);


    /**
     * 获取角色列表
     * @param homeId
     * @return
     */
    @Select("SELECT * FROM smarthome.home_members hm " +
            "LEFT JOIN smarthome.homes h ON h.id = hm.home_id " +
            "LEFT JOIN smarthome.users u ON u.id = hm.user_id " +
            "WHERE hm.home_id = #{homeId}")
    List<HomeMembersVO> queryHomeMemberInfo(Long homeId);

    /**
     * 删除家庭成员
     * @param homeId
     */
    @Delete("DELETE FROM smarthome.home_members WHERE home_id = #{homeId}")
    void DeleteHome(Long homeId);

    /**
     * 退出家庭
     * @param homeId
     * @param userId
     */
    @Delete("delete from smarthome.home_members where home_id = #{homeId} And user_id = #{userId}")
    void LogoutHome(Long homeId, Long userId);
}
